package com.zhaungjie.base.utils.PropertiesCopyUtils;

import com.zhaungjie.base.utils.LambdaStructureParser.LambdaStructureParser;
import com.zhaungjie.base.utils.PropertiesCopyUtils.function.ProcessFunction;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体dto转换器
 *
 * @author zhuangjie
 * @date 2023/04/26
 */
@Getter
public class PropertiesCopyConfig<S,T> {
    private List<String> ignorePropertiesName;
    private Map<String,String> propertiesMap;
    private Map<String,ProcessFunction<S>> processMap;

    /**
     * 忽略属性
     *
     * @param nSFunction n个函数
     * @return {@link PropertiesCopyConfig}
     */
    public PropertiesCopyConfig<S,T> ignoreProperties(LambdaStructureParser.NANRFunction<T>... nSFunction) {
        if (ignorePropertiesName == null) ignorePropertiesName = new ArrayList<>();
        for (LambdaStructureParser.NANRFunction<T> sFunctions : nSFunction) {
            ignorePropertiesName.add(LambdaStructureParser.convertToFieldName(sFunctions));
        }
        return this;
    }

    /**
     * 拷贝属性映射
     *
     * @param sourceSFunction 源属性
     * @param targetSFunction 目标属性
     * @return {@link PropertiesCopyConfig}
     */
    public PropertiesCopyConfig<S,T> map(LambdaStructureParser.NAHRFunction<S,?> sourceSFunction, LambdaStructureParser.NAHRFunction<T,?> targetSFunction) {
        if (sourceSFunction == null || targetSFunction == null) return this;
        if (propertiesMap == null) propertiesMap = new HashMap<>();
        propertiesMap.put(LambdaStructureParser.convertToFieldName(sourceSFunction), LambdaStructureParser.convertToFieldName(targetSFunction));
        return this;
    }


    public PropertiesCopyConfig<S,T> valueAdapter(LambdaStructureParser.NANRFunction<S> sourceSFunction, ProcessFunction<S> processFunction) {
        if (processMap == null) processMap = new HashMap<>();
        processMap.put(LambdaStructureParser.convertToFieldName(sourceSFunction),processFunction);
        return this;
    }


}
